/**
* 树内部脚本
*/

function newXMLHttpRequest() {
  var xmlreq = null;
  if (window.XMLHttpRequest) {
	 xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      try {
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
      }
    }
  }
  return xmlreq;
};

// 默认点击事件
function default_click(nodeObj){
	// nothing
};

// 树对象
var tree = null;
function Tree(id, containerId) {
	this.id = id;
	this.containerId = containerId;
	var nodes = new Array();
	var childrenLoaded = new Array(); // 子节点是否已经加载过
	var req = null;
	var container = null;
	var containerSuffix = "_div";
	var selectedNodeDom = null; // 当前被选中的节点
	
	// 初始化
	this.init = function(){
		req = newXMLHttpRequest();
		container = document.getElementById(this.containerId);
	};
	
	/**
	 * 获取子节点
	 * pid:父节点id
	 * isClickText：点击是否是文本，用于文本选中
	 */
	this.viewChildNodes = function(pid, isClickText) {
		
		if(isClickText){// 点击的是文本区，则选中此区域
			var pNodeDom = document.getElementById(pid);
			if(pNodeDom) {
				selected(pNodeDom);
			}
		}
		
		// 叶子节点不请求加载
		if(nodes[pid] && nodes[pid].isLeaf){
			return;
		}
		
		// 是否已经加载过
		if(childrenLoaded[pid]){
			var pNode = nodes[pid];
			// 只是显示,不加载
			if(pNode.isOpen){
				this.close(pid);
			}else {
				this.open(pid);
			}
			return;
		}
		
		var url="treeServlet?treeId=" + tree.id + "&pid=" + pid;
		req.onreadystatechange = function(){
			viewCallBack(pid);
		};
		req.open("GET",url,true);
		req.send(null);
	};
	
	// 获取子节点回调
	function viewCallBack(pid){
		if(req.readyState == 4){//HTTP就绪状态，响应已完成，可以访问服务器响应并使用它
		     if(req.status == 200){//服务器状态码
		       //var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		       var xmlDoc = req.responseXML;
		       // 显示节点
		       var htmlNode = xmlDoc.selectSingleNode("//tree/html");
		       
		       var childContainer = null; // 页面dom对象
		       
		       if(pid == ""){// 根节点初始化
		    	   childContainer = $(container);
		       }else {
		    	   childContainer = $(document.getElementById(pid + containerSuffix));
		       }
		       
		       // 显示子节点
		       childContainer.append(htmlNode.text);
		       
		       // 修改父节点图标
		       var pNodeDom = document.getElementById(pid);
		       if(pNodeDom) {
		    	   setNodeImg(pNodeDom, true);
		       }
		       
		       // 加载节点内存信息
		       var treeNodes = xmlDoc.selectNodes("//tree/treeNodes/treeNode");
		       for(var i = 0 ; i < treeNodes.length; i++) {
		    	   var node = new TreeNode();
		    	   node.setArgs(treeNodes[i].text);
		    	   nodes[node.id] = node;
		       }
		       
		       // 添加加载标记
		       if(nodes[pid]) {
		    	   childrenLoaded[pid] = true;
		    	   nodes[pid].isOpen = true;
		       }
		     }
	    }	
	};
	
	// 获取所有节点对象
	this.getAllNodes = function(){
		return nodes;
	};
	
	// 展开
	this.open = function(id) {
		var childContainer = $(document.getElementById(id + containerSuffix));
		childContainer.css("display", "");
		nodes[id].isOpen = true;
		
		// 更换图标
		var curNode = document.getElementById(id);
		setNodeImg(curNode, true);
		
	};
	
	// 展开节点
	function setNodeImg(nodeDom, isOpen) {
		var nodeObj = nodes[nodeDom.id];
		if(nodeObj.isLeaf) {
			return;
		}
		
		if(isOpen) {
			// 展开图标
			var iconObj = nodeDom.childNodes(0).childNodes(0).childNodes(0).childNodes(0);
			var iconSrc = iconObj.src.replace("plus", "sub");
			iconObj.src = iconSrc;
			
			// 文件夹图标
			var imgObj = nodeDom.childNodes(0).childNodes(0).childNodes(1).childNodes(0);
			var imgSrc = imgObj.src.replace("folder", "folder_open"); 
			imgObj.src = imgSrc;
		}else {
			// 展开图标
			var iconObj = nodeDom.childNodes(0).childNodes(0).childNodes(0).childNodes(0);
			var iconSrc = iconObj.src.replace("sub", "plus");
			iconObj.src = iconSrc;
			
			// 文件夹图标
			var imgObj = nodeDom.childNodes(0).childNodes(0).childNodes(1).childNodes(0);
			var imgSrc = imgObj.src.replace("folder_open", "folder"); 
			imgObj.src = imgSrc;
		}
	};
	
	// 收起
	this.close = function(id) {
		var childContainer = $(document.getElementById(id + containerSuffix));
		childContainer.css("display", "none");
		nodes[id].isOpen = false;
		
		// 更换图标
		var curNode = document.getElementById(id);
		setNodeImg(curNode, false);
	};
	
	// 选中
	function selected(nodeDom){
		
		// 修改之前选中节点为未选中
		var txtDom = null;
		if(selectedNodeDom){
			txtDom = selectedNodeDom.childNodes(0).childNodes(0).childNodes(2);
			$(txtDom).css("background-color","");
		}
		
		txtDom = nodeDom.childNodes(0).childNodes(0).childNodes(2);
		// 设置当前节点选中
		selectedNodeDom = nodeDom;
		$(txtDom).css("background-color","#a3d4ff");
	};
};

// 图表内部事件转发器
function dispatch(handler, nodeArgs){
	var args = arguments;
	if(args && args.length > 1){
		handler.chartArguments = nodeArgs;
		var node = new TreeNode();
		node.setArgs(nodeArgs);
		tree.viewChildNodes(node.id, true);
		handler.call(handler, node);
	}else {
		handler.call();
	} 
		
}

// 图表对象
function TreeNode(){
	this.nodeArgs = "";// 图表参数
	
	//this.separator = "&"; // 参数分隔符
	this.separator = "@_@"; // 参数分隔符
	this.separator_equal = "~"; // 参数赋值符
	
	this.argNames = new Array();// 参数名
	this.argValues = new Array();// 参数值
	
	this.id = "";
	this.pid = "";
	this.name = "";
	this.title = "";
	this.next = "";
	this.pre = "";
	this.level = 0;
	this.isLeaf = false;
	this.isOpen = false;
		
	// 设置图表参数
	this.setArgs = function(args) {
		this.nodeArgs = args;
		this.separateArgs();
		this.id = this.getArgValue("id");
		this.pid = this.getArgValue("pid");
		this.name = this.getArgValue("name");
		this.title = this.getArgValue("title");
		this.next = this.getArgValue("next");
		this.pre = this.getArgValue("pre");
		this.level = this.getArgValue("level");
		this.isLeaf = ("true" == this.getArgValue("isLeaf"));
	};
	
	// 分解参数
	this.separateArgs = function(){
		var allArgs = this.nodeArgs.split(this.separator);
		var args ;
		for(var i = 0; i < allArgs.length; i++){
			args = allArgs[i].split(this.separator_equal);
			this.argNames[i] = args[0];
			if(args.length == 2){
				this.argValues[args[0]] = args[1];
			}else {
				this.argValues[args[0]] = "";
			}
		}
	};
	
	this.getInfo = function(name) {
		return this.getArgValue(name);
	};
	
	// 获取所有图表参数
	this.getAllArgs = function(){
		return this.nodeArgs;
	};
	
	// 获取指定参数值
	this.getArgValue = function(argName){
		return this.argValues[argName];
	};
	
	// 获取所有参数名
	this.getArgNames = function(){
		return this.argNames;
	};
}

